package com.dvd.djk.gdjk002;
import org.hibernate.criterion.*;
import org.hibernate.*;
import java.util.List;

import org.hibernate.Transaction;

import com.dvd.dao.dvd.TDvd;
import com.dvd.dao.dvd.TDvdDAO;

public class GDJK002Model {

	@SuppressWarnings("unchecked")
	public List<TDvd> searchDvdList(GDJK002Bean bean) {
		String FDvdTitle = bean.getFDvdTitle();
		String FYear = bean.getFYear();
		String FType = bean.getFType();
		String FCountry = bean.getFCountry();
		String FPrice = bean.getFPrice();
		String FActor = bean.getFActor();
		String FDirector = bean.getFDirector();
		float lowPrice=0f;
		float highPrice=0f;
		if(FPrice.equals("２万円以上"))
		{
			lowPrice=20000;
			highPrice=999999;
		}
		else if(FPrice.equals("１万円　～　２万円"))
		{
			lowPrice=10000;
			highPrice=20000;
		}
		else if(FPrice.equals("５千円　～　１万円"))
		{
			lowPrice=5000;
			highPrice=10000;
		}
		else if(FPrice.equals("５千円以下"))
		{
			lowPrice=0;
			highPrice=5000;
		}
 		List<TDvd> list=null;
		TDvdDAO dao = new TDvdDAO();
		Transaction tran = dao.getSession().beginTransaction();
		Criteria criteria = dao.getSession().createCriteria(TDvd.class)
		.add(Expression.ilike("FDvdTitle","%" +FDvdTitle+ "%")).
		add(Expression.ilike("FType","%" +FType+ "%")).
		add(Expression.ilike("FCountry","%" +FCountry+ "%")).
		add(Expression.ilike("FActor","%" +FActor+ "%")).
		add(Expression.ilike("FDirector","%" +FDirector+ "%")).
		add(Expression.ge("FPrice", lowPrice)).
		add(Expression.lt("FPrice", highPrice));
		if(FYear!="")
		{
			criteria.add(Expression.eq("FYear", Integer.parseInt(FYear)));
		}
		list = (List<TDvd>)criteria.list();
		tran.commit();
		dao.getSession().close();
		return list;
	}
	public int getPageCount(int perPageCount)
	{
		int count = -1;
		TDvdDAO dao=new TDvdDAO();		
		Transaction trans =dao.getSession().beginTransaction();
		count = dao.findAll().size()/perPageCount + 1;
		trans.commit();
		dao.getSession().close();
		return count;
	}
	
	public static void main(String [] args)
	{
		GDJK002Model model = new GDJK002Model();
		GDJK002Bean bean= new GDJK002Bean();
		bean.setFDvdTitle("dvd");
		bean.setFPrice("１万円　～　２万円");
		bean.setFYear("1700");
		List<TDvd> dvds = model.searchDvdList(bean);
		System.out.println(dvds.size());
	}

}
